/******************/
/* POB-Technology */
/* PobLCDemo.c    */
/* 07/07/2005	  */
/******************/


#include "POB-EYE.h"
#include "Intro.h"
#include "demo.h"

void IntroPOB(void);

#define LCD_HEIGHT 64
#define LCD_WIDTH	128

UInt8 LCD_Buffer[LCD_HEIGHT*LCD_WIDTH*BITS];
GraphicBuffer LCD_BufferB;




#define TINYPOBEYE_X	93
#define TINYPOBEYE_Y	24	

#define TITLE_X		26
#define TITLE_Y		2

#define SHADOW0_X	9
#define SHADOW0_Y	52

#define SHADOW1_X	53
#define SHADOW1_Y	52

#define SHADOW2_X	94
#define SHADOW2_Y	52


#define BATTERY_X	52
#define BATTERY_Y	29

#define BLOCNOTE_X	8
#define BLOCNOTE_Y	25

#define CURSOR0_X   6
#define CURSOR0_Y	4

#define CURSOR1_X   42
#define CURSOR1_Y	44


#define CURSOR2_X   97  
#define CURSOR2_Y	18


#define CURSOR3_X	111       
#define CURSOR3_Y	45

#define FRAME_X		90
#define FRAME_Y		22


void DrawMenu(void);
void MoveCursor(Point *Start,Point *End);
Point Cursor;

#define HAT_START_X 100
#define HAT_START_Y	45
	
#define HAT_END_X	30	
#define HAT_END_Y	8

#define EYEBROWS1_START_X	75
#define EYEBROWS1_START_Y	45

#define EYEBROWS1_END_X	17
#define EYEBROWS1_END_Y	25

#define EYEBROWS2_START_X	18
#define EYEBROWS2_START_Y	24

#define EYEBROWS2_END_X		
#define EYEBROWS2_END_Y		

#define FOOT_START_X 70
#define FOOT_START_Y 18

#define FOOT_END_X 17
#define FOOT_END_Y 44

#define HARM1_START_X 113
#define HARM1_START_Y 11

#define HARM1_END_X 50
#define HARM1_END_Y 30

#define HARM2_START_X	51
#define HARM2_START_Y	17

#define HARM2_END_X	
#define HARM2_END_Y

#define POBEYE_START_X 20
#define POBEYE_START_Y 14

#define POBEYE_END_X 
#define POBEYE_END_Y 







Point POBEYE;
Point HARM1;
Point HARM2;
Point FOOT;
Point EYEBROWS1;
Point EYEBROWS2;
Point HAT;

void DrawPiece(void);
void MovePicture(Point *Start,Point *End);
void DrawPOBEYE(void)
{
	
	Point End;
	POBEYE.x=POBEYE_START_X;
	POBEYE.y=POBEYE_START_Y;

	HARM1.x=HARM1_START_X;
	HARM1.y=HARM1_START_Y;

	HARM2.x=HARM2_START_X;
	HARM2.y=HARM2_START_Y;

	FOOT.x=FOOT_START_X;
	FOOT.y=FOOT_START_Y;

	EYEBROWS1.x=EYEBROWS1_START_X;
	EYEBROWS1.y=EYEBROWS1_START_Y;

	EYEBROWS2.x=EYEBROWS2_START_X;
	EYEBROWS2.y=EYEBROWS2_START_Y;
	
	HAT.x=HAT_START_X;
	HAT.y=HAT_START_Y;

	
	
	//clear the left screen	
	ClearGraphicBuffer(&LCD_BufferB);

	//put the differents pieces of body
	DrawPiece();
	
	//draw the screen
	DrawLCD(&LCD_BufferB);
	

	Wait(1000000);


	End.x=HAT_END_X;
	End.y=HAT_END_Y;	
	MovePicture(&HAT,&End);

	End.x=FOOT_END_X;
	End.y=FOOT_END_Y;	
	MovePicture(&FOOT,&End);

	End.x=HARM1_END_X;
	End.y=HARM1_END_Y;	
	MovePicture(&HARM1,&End);

	End.x=EYEBROWS1_END_X;
	End.y=EYEBROWS1_END_Y;	
	MovePicture(&EYEBROWS1,&End);

	Wait(800000);	
	ClearGraphicBuffer(&LCD_BufferB);
	DrawBitmap(POBEYE.x,POBEYE.y,IDB_POBEYE,demo,&LCD_BufferB);
	DrawBitmap(EYEBROWS2.x,EYEBROWS2.y,IDB_EYEBROW2,demo,&LCD_BufferB);
	DrawBitmap(FOOT.x,FOOT.y,IDB_FOOT,demo,&LCD_BufferB);
	DrawBitmap(HARM2.x,HARM2.y,IDB_HARM2,demo,&LCD_BufferB);
	DrawBitmap(HAT.x,HAT.y,IDB_HAT,demo,&LCD_BufferB);
	DrawLCD(&LCD_BufferB);

	Wait(1000000);

	POBEYE.x-=68;
	HARM1.x-=68;
	HARM2.x-=68;
	FOOT.x-=68;
	EYEBROWS1.x-=68;
	EYEBROWS2.x-=68;
	HAT.x-=68;

	ClearGraphicBuffer(&LCD_BufferB);
	DrawBitmap(POBEYE.x,POBEYE.y,IDB_POBEYE,demo,&LCD_BufferB);
	DrawBitmap(EYEBROWS2.x,EYEBROWS2.y,IDB_EYEBROW2,demo,&LCD_BufferB);
	DrawBitmap(FOOT.x,FOOT.y,IDB_FOOT,demo,&LCD_BufferB);
	DrawBitmap(HARM2.x,HARM2.y,IDB_HARM2,demo,&LCD_BufferB);
	DrawBitmap(HAT.x,HAT.y,IDB_HAT,demo,&LCD_BufferB);
	DrawLCD(&LCD_BufferB);
	

	DrawBitmap(0,0,IDB_TEXT1,demo,&LCD_BufferB);
	DrawLCD(&LCD_BufferB);


	Wait(2000000);
	ClearGraphicBuffer(&LCD_BufferB);
	DrawBitmap(POBEYE.x,POBEYE.y,IDB_POBEYE,demo,&LCD_BufferB);
	DrawBitmap(EYEBROWS2.x,EYEBROWS2.y,IDB_EYEBROW2,demo,&LCD_BufferB);
	DrawBitmap(FOOT.x,FOOT.y,IDB_FOOT,demo,&LCD_BufferB);
	DrawBitmap(HARM2.x,HARM2.y,IDB_HARM2,demo,&LCD_BufferB);
	DrawBitmap(HAT.x,HAT.y,IDB_HAT,demo,&LCD_BufferB);
	DrawLCD(&LCD_BufferB);	

	DrawBitmap(0,0,IDB_TEXT2,demo,&LCD_BufferB);
	DrawLCD(&LCD_BufferB);


	Wait(1000000);
	HAT.y-=5;
	ClearGraphicBuffer(&LCD_BufferB);
	DrawBitmap(POBEYE.x,POBEYE.y,IDB_POBEYE,demo,&LCD_BufferB);
	DrawBitmap(EYEBROWS2.x,EYEBROWS2.y,IDB_EYEBROW2,demo,&LCD_BufferB);
	DrawBitmap(FOOT.x,FOOT.y,IDB_FOOT,demo,&LCD_BufferB);
	DrawBitmap(HARM2.x,HARM2.y,IDB_HARM2,demo,&LCD_BufferB);
	DrawBitmap(HAT.x,HAT.y,IDB_HAT,demo,&LCD_BufferB);
	DrawLCD(&LCD_BufferB);	

	DrawBitmap(0,0,IDB_TEXT2,demo,&LCD_BufferB);
	DrawLCD(&LCD_BufferB);


	Wait(700000);
	HAT.y+=5;
	ClearGraphicBuffer(&LCD_BufferB);
	DrawBitmap(POBEYE.x,POBEYE.y,IDB_POBEYE,demo,&LCD_BufferB);
	DrawBitmap(EYEBROWS2.x,EYEBROWS2.y,IDB_EYEBROW2,demo,&LCD_BufferB);
	DrawBitmap(FOOT.x,FOOT.y,IDB_FOOT,demo,&LCD_BufferB);
	DrawBitmap(HARM2.x,HARM2.y,IDB_HARM2,demo,&LCD_BufferB);
	DrawBitmap(HAT.x,HAT.y,IDB_HAT,demo,&LCD_BufferB);
	DrawLCD(&LCD_BufferB);	

	DrawBitmap(0,0,IDB_TEXT2,demo,&LCD_BufferB);
	DrawLCD(&LCD_BufferB);


}



int main (void)
{
	UInt8 i=0;
	Point End;



	InitGraphicBuffer(&LCD_BufferB,128,64,ONE_BIT,LCD_Buffer); 


	//Initialize the system
	InitPOBEYE();		

	
	InitLCD();	//Init the LCD Screen
	IntroPOB();
	//DrawPOBEYE();
	
	Cursor.x=CURSOR0_X;
	Cursor.y=CURSOR0_Y;
	
		
	End.x=CURSOR1_X;
	End.y=CURSOR1_Y;
	MoveCursor(&Cursor,&End);


	End.x=CURSOR2_X;
	End.y=CURSOR2_Y;
	MoveCursor(&Cursor,&End);

	End.x=CURSOR3_X;
	End.y=CURSOR3_Y;
	MoveCursor(&Cursor,&End);


	//Wait(500000);
	DrawMenu();
	
	
	DrawBitmap(FRAME_X,FRAME_Y,IDB_FRAME,demo,&LCD_BufferB);
	DrawLCD(&LCD_BufferB);
	Wait(500000);
	DrawPOBEYE();

	while (1);

}




void DrawMenu(void)
{
	ClearGraphicBuffer(&LCD_BufferB);
	DrawBitmap(TINYPOBEYE_X,TINYPOBEYE_Y,IDB_TINYPOBEYE,demo,&LCD_BufferB);
	DrawBitmap(BATTERY_X,BATTERY_Y,IDB_BATTERY,demo,&LCD_BufferB);
	DrawBitmap(TITLE_X,TITLE_Y,IDB_TITLE,demo,&LCD_BufferB);
	DrawBitmap(BLOCNOTE_X,BLOCNOTE_Y,IDB_BLOCNOTE,demo,&LCD_BufferB);
	
	DrawBitmap(SHADOW0_X,SHADOW0_Y,IDB_SHADOW,demo,&LCD_BufferB);
	DrawBitmap(SHADOW1_X,SHADOW1_Y,IDB_SHADOW,demo,&LCD_BufferB);
	DrawBitmap(SHADOW2_X,SHADOW2_Y,IDB_SHADOW,demo,&LCD_BufferB);

	DrawBitmap(Cursor.x,Cursor.y,IDB_ARROW,demo,&LCD_BufferB);
	DrawLCD(&LCD_BufferB);
	Wait(10000);

}


void DrawPiece(void)
{
	ClearGraphicBuffer(&LCD_BufferB);
	DrawBitmap(POBEYE.x,POBEYE.y,IDB_POBEYE,demo,&LCD_BufferB);
	DrawBitmap(EYEBROWS1.x,EYEBROWS1.y,IDB_EYEBROW1,demo,&LCD_BufferB);
	DrawBitmap(FOOT.x,FOOT.y,IDB_FOOT,demo,&LCD_BufferB);
	DrawBitmap(HARM1.x,HARM1.y,IDB_HARM1,demo,&LCD_BufferB);
	DrawBitmap(HAT.x,HAT.y,IDB_HAT,demo,&LCD_BufferB);
	DrawLCD(&LCD_BufferB);
	Wait(70000);

}


void MovePicture(Point *Start,Point *End)
{
	Int16		x, y;
	Int16		dx, dy;
	Int16		incx, incy;
	Int16		balance;
	UInt8 x1=Start->x,	x2=End->x,	y1=Start->y,		y2=End->y;
	
	

	if (x2 >= x1)
	{
		dx = x2 - x1;
		incx = 1;
	}
	else
	{
		dx = x1 - x2;
		incx = -1;
	}

	if (y2 >= y1)
	{
		dy = y2 - y1;
		incy = 1;
	}
	else
	{
		dy = y1 - y2;
		incy = -1;
	}

	x = x1;
	y = y1;

	if (dx >= dy)
	{
		dy <<= 1;
		balance = dy - dx;
		dx <<= 1;

		while (x != x2)
		{		
			
			Start->x=x;
			Start->y=y;
			DrawPiece();


			if (balance >= 0)
			{
				y += incy;
				balance -= dx;
			}
			balance += dy;
			x += incx;
		} 
		Start->x=x;
		Start->y=y;
		DrawPiece();
	}
	else
	{
		dx <<= 1;
		balance = dx - dy;
		dy <<= 1;

		while (y != y2)
		{
			Start->x=x;
			Start->y=y;
			DrawPiece();
			if (balance >= 0)
			{
				x += incx;
				balance -= dy;
			}
			balance += dx;
			y += incy;
		} 
		Start->x=x;
		Start->y=y;
		DrawPiece();
	}

}

void MoveCursor(Point *Start,Point *End)
{
	Int16		x, y;
	Int16		dx, dy;
	Int16		incx, incy;
	Int16		balance;
	UInt8 x1=Start->x,	x2=End->x,	y1=Start->y,		y2=End->y;
	
	

	if (x2 >= x1)
	{
		dx = x2 - x1;
		incx = 1;
	}
	else
	{
		dx = x1 - x2;
		incx = -1;
	}

	if (y2 >= y1)
	{
		dy = y2 - y1;
		incy = 1;
	}
	else
	{
		dy = y1 - y2;
		incy = -1;
	}

	x = x1;
	y = y1;

	if (dx >= dy)
	{
		dy <<= 1;
		balance = dy - dx;
		dx <<= 1;

		while (x != x2)
		{		
			
			Start->x=x;
			Start->y=y;
			DrawMenu();


			if (balance >= 0)
			{
				y += incy;
				balance -= dx;
			}
			balance += dy;
			x += incx;
		} 
		Start->x=x;
		Start->y=y;
		DrawMenu();
	}
	else
	{
		dx <<= 1;
		balance = dx - dy;
		dy <<= 1;

		while (y != y2)
		{
			Start->x=x;
			Start->y=y;
			DrawMenu();
			if (balance >= 0)
			{
				x += incx;
				balance -= dy;
			}
			balance += dx;
			y += incy;
		} 
		Start->x=x;
		Start->y=y;
		DrawMenu();
	}

}


void IntroPOB(void)
{
	UInt32 i=0;
	
	//clear the right screen
	ClearGraphicBuffer(&LCD_BufferB);
	
	//Draw the 3 letters
	DrawBitmap(8,5,IDB_POB,intro,&LCD_BufferB);
	DrawLCD(&LCD_BufferB);
	Wait(1000000);

	DrawBitmap(8,47,IDB_TECHNO,intro,&LCD_BufferB);		
	DrawLCD(&LCD_BufferB);	
	
	Wait(3000000);
}
